const Router = require("koa-router");
const api = require("../database");
const router = new Router();
router.get("/", async function (ctx) {
    ctx.body = "welcome";
});
router.get("/user", async function (ctx) {
    ctx.body = "welcome to user";
});
router.post("/user", async function (ctx) {
    ctx.body = "welcome to post";
});
router.delete("/user", async function (ctx) {
    ctx.body = "welcome to delete";
});
router.put("/user", async function (ctx) {
    ctx.body = "welcome to put";
});
router.get("/serviceError", async function (ctx) {
    dsdsds
    ctx.body = "执行错误";
});

const sleep = function () {
    return new Promise(function (resolve, reject) {
        setTimeout(resolve, 3000);
    });
};
router.get("/userAsync", async function (ctx) {
    await sleep();
    let user = {
        name: "花花",
        age: 23,
    };
    ctx.body = user;
});

let users = [
    {
        id: 1001,
        name: "alice",
        age: 20
    },
    {
        id: 1002,
        name: "tom",
        age: 20
    },
    {
        id: 1003,
        name: "cici",
        age: 20
    }
];

router.get("/users", async function (ctx) {
    ctx.body = users;//响应数据
});

router.get("/getUser", async function (ctx) {
    ctx.body = users;//响应数据
});
/* 
请求路径/:参数名/:参数名...
koa-router为ctx提供一个特殊的参数数据,ctx.params,该属性时专用于路径参数的获取 */
router.get("/getUser/:id", async function (ctx) {


    let index = ctx.params.id;
    if (index >= users.length) {
        ctx.body = "没有该用户";
    } else {
        ctx.body = users[index];
    }

});

router.get("/getUser/:id/:name", async function (ctx) {

    console.log(ctx.params);//获取参数
    ctx.body = ctx.params;

});

router.get("/getUsers", async function (ctx) {
    let rows = api.findAllUsers();
    ctx.body = rows;
});

router.get("/queryUser/:id", async function (ctx) {
    console.log(ctx.params);
    let id = ctx.params.id;
    let row = api.findOneUser(id);
    if (row) {//如果有该用户
        ctx.body = {
            flag: true,
            user: row
        };
    } else {
        ctx.body = {
            flag: false
        }
    }

});

router.get("/header", async function (ctx) {
    console.log(ctx.headers);//获取所有的请求头信息
    console.log(ctx.headers['x-requested-with']);
    ctx.body = {
        msg: "请求头参数获取成功"
    };
});
router.get("/query", async function (ctx) {
    console.log(ctx.query);

    ctx.body = {
        msg: "请求头参数获取成功"
    };
});

router.get("/insertClass", async function (ctx) {
    let classObj = ctx.query;//对象
    console.log(classObj);
    api.insertClass(classObj);
    ctx.body = {
        msg: "插入成功"
    };
});

router.post("/body", async function (ctx) {

    console.log(ctx.request.body);
    console.log(ctx.request.files);
    ctx.body = {
        msg: "请求头参数获取成功"
    };
});
router.get("/html", async function (ctx) {

    let html = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        welcome to a
    </body>
    </html>
    `;

    // koa服务器路由中定义响应头
    ctx.type="html";

    //定义响应头，通知浏览器内容类型是普通文本
    // ctx.type="text";
    ctx.body = html;
});
router.get("/showStu", async function (ctx) {
    let stus = api.findAllStu();
    ctx.body = stus;
});
router.get("/showClass", async function (ctx) {
    let classArr = api.findAllClass();
    ctx.body = classArr;
});

router.post("/uploadFile", async function (ctx) {

    let stu = ctx.request.body;
    let filename = ctx.request.files.img.newFilename;
    stu.img=filename;
    api.insertStu(stu);
    ctx.body = {
        msg: "请求头参数获取成功"
    };
});

router.get("/text",async function(ctx){
    ctx.type="text";
    ctx.body="数据1,数据2,数据3,数据4";
});
router.get("/htmlStr",async function(ctx){
    ctx.type="html";
    ctx.body=`
        <li>数据1</li>
        <li>数据2</li>
        <li>数据3</li>
        <li>数据4</li>
    `;
});
router.get("/json",async function(ctx){
    ctx.type="json";
    ctx.body=["数据1","数据2","数据3","数据4"];
});


module.exports = {
    "ajaxRouter": router
};